$(function () {
    var getId=GetRequest();
    var openid= getId['openid'];
    var nickname= getId['nickname'];
    var avatar= getId['avatar'];
    console.log(openid); //打印出传过来的id*/
    function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }
    if (!openid) {
        window.location.href = 'http://api.mtaction.com/dian/Qm/carve/auto.php?url=http://api.mtaction.com/dian/Qm/carve/getopen.php';
    }else{
        $('.content').show();
    }
        window.formData = {
            name: null,
            branch: null,
            openid: openid,
            nickname: nickname,
            avatar: avatar,
        };
    var str = '';
    $.get('http://slave.mtaction.com/api/branches',
        function (data) {
            for(var i = 0;i < data.length;i++){
                var branch = (data[i].name);
                str += "<option value="+data[i].id+">"+branch+"</option>";
            }
            $("#branch").append(str);
        });
});
$('#name').change(function(){
    var myname = /^[\u4e00-\u9fa5]{2,5}|[a-zA-Z ]{2,20}$/;
    var name = $(this).val();
    if (!myname.test(name)) {
        $('.xing').css('display','block');
        $('.xing').text('至少两个汉字或英文');
    }else{
        $('.xing').css('display','none');
        localStorage.setItem('name',name);
    }
});
localStorage.setItem('url','http://slave.mtaction.com/api/members/create');
$.get('http://slave.mtaction.com/api/members',
    function (data) {
        for (var i = 0; i < data.length; i++) {
           if (window.formData.openid == data[i].openid) {
               localStorage.setItem('name',data[i].name);
               $('#name').val(data[i].name);
               $('#branch').val(data[i].branch_id);
               $('#username').val(data[i].username);
               localStorage.setItem('url','http://slave.mtaction.com/api/members/update');
           }
        }
    });
$(function() {
    $('#tijiao').click(function () {
        var name = localStorage.getItem('name');
        var branch = $('#branch option:selected').val();
        var username = $('#username').val();
        var password = $('#password').val();
        var openid = window.formData.openid;
        var avatar = window.formData.avatar;
        var nickname = window.formData.nickname;
console.log(name,branch,username,password,openid,avatar,nickname);
        $.post(localStorage.getItem('url'), {//注册接口
            openid: openid,
            name: name,
            branch_id: branch,
            username:username,
            password:password,
            avatar: avatar,
            nickname: nickname,
        }, function (data) {
            if (data.username) {
                alert('账号已注册');
            }else if(data.branch_id)
            {
                alert('请选择门派');
            } else if(data.password) {
                alert('密码不能为空');
            } else {
                alert("提交审核成功!!!");
                window.location.href = 'http://slave.mtaction.com/mtshare-web/success.html';
            }
        });
    });

});
